package com.ext.aic.apis.content;

import android.content.Context;

import androidx.lifecycle.LifecycleOwner;

import com.ext.aic.apis.ApiDefine;
import com.ext.aic.apis.ApiPaths;
import com.ext.aic.builder.ParamsBuilder;
import com.ext.aic.apis.RequestResponse;
import com.ext.aic.beans.Pagination;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.beans.params.ProductListParams;
import com.ext.aic.beans.response.ProductResponse;
import com.eyinfo.android_pure_utils.utils.ConvertUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.config.IRequestApi;

import java.util.List;

public class ProductListRequest {

    public void onProductListSuccess(List<ProductItem> data, Pagination pagination) {

    }

    public void onProductListEnd() {

    }

    public void request(Context context, LifecycleOwner lifecycleOwner, ProductListParams params, boolean isShowLoading) {
        ParamsBuilder builder = new ParamsBuilder();
        builder.add("title", params.getTitle());
        builder.add("contentStatus", ConvertUtils.toJoin(params.getContentStatus(), ","));
        builder.add("page", params.getPage());
        builder.add("pageSize", params.getPageSize());
        ApiDefine apiDefine = new ApiDefine(ApiPaths.contentList, builder);
        EasyHttp.get(lifecycleOwner).api(apiDefine).request(new RequestResponse<ProductResponse>(context, isShowLoading) {
            @Override
            protected void onSuccess(ProductResponse response) {
                onProductListSuccess(response.getData(), response.getPagination());
            }

            @Override
            public void onHttpEnd(IRequestApi api) {
                super.onHttpEnd(api);
                onProductListEnd();
            }
        });
    }
}
